//
//  STLoginManager.swift
//  iPhone
//
//  Created by Toj on 3/31/23.
//

import Foundation

class STLoginManager: NSObject {
    static let manager = STLoginManager()
}

extension STLoginManager {
    
    func responseLogin(user: STUserModel) {
        STUserCenter.default.setupUser(info: user)
    }
}

extension STLoginManager {
    
    func request(login completion: @escaping (()->Void)) {
        let deadline = DispatchTime.now() + Double(arc4random_uniform(2000)) / 1000
        DispatchQueue.main.asyncAfter(deadline: deadline) {
            print("-->toj: 登录成功 func:" + #function)
            STUserCenter.default.isLogin = true
            NotificationCenter.default.post(name: .loginSuccessNotification, object: nil)
            completion()
        }
    }
    
    func request(autoLogin completion: (()->Void)? = nil) {
        let deadline = DispatchTime.now() + Double(arc4random_uniform(2000)) / 1000 // + 3
        DispatchQueue.main.asyncAfter(deadline: deadline) {
            print("-->toj: 自动登录成功 func:" + #function)
            STUserCenter.default.isLogin = true
            NotificationCenter.default.post(name: .loginSuccessNotification, object: nil)
            completion?()
        }
    }
    
    func request(thirdLogin completion: @escaping (()->Void)) {
        let deadline = DispatchTime.now() + Double(arc4random_uniform(2000)) / 1000
        DispatchQueue.main.asyncAfter(deadline: deadline) {
            print("-->toj: 第三方登录成功 func:" + #function)
            STUserCenter.default.isLogin = true
            NotificationCenter.default.post(name: .loginSuccessNotification, object: nil)
            completion()
        }
    }
}
